//===============================================================================
// Microsoft patterns & practices
// Web Client Software Factory
//-------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//-------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using System.Workflow.Runtime;

namespace WCSFContrib.PageFlow.WorkflowFoundation.Activities
{
	/// <summary>
	/// An event driven <see cref="Activity">Activity</see> that allows transitions
	/// </summary>
	public class TransitionListenerActivity : Activity, IEventActivity
	{
		/// <summary>
		/// Gets the queue name
		/// </summary>
		public IComparable QueueName
		{
			get
			{
				return Parent.Name.ToUpper(CultureInfo.CurrentCulture);
			}
		}

		/// <summary>
		/// Creates the subscription of an Activity to an event. 
		/// </summary>
		/// <param name="parentContext">The ActivityExecutionContext that represents the execution environment of the Activity.</param>
		/// <param name="parentEventHandler">The EventHandler for the parent event.</param>
		public void Subscribe(ActivityExecutionContext parentContext,
		                      IActivityEventListener<QueueEventArgs> parentEventHandler)
		{
			WorkflowQueuingService queueService =
				(WorkflowQueuingService) parentContext.GetService(typeof (WorkflowQueuingService));
			WorkflowQueue eventQueue = null;
			eventQueue = queueService.CreateWorkflowQueue(QueueName, true);
			eventQueue.RegisterForQueueItemAvailable(parentEventHandler, QualifiedName);
		}

		/// <summary>
		/// Cancels the subscription of an Activity to an event. 
		/// </summary>
		/// <param name="parentContext">The ActivityExecutionContext that represents the execution environment of the Activity.</param>
		/// <param name="parentEventHandler">The EventHandler for the parent event.</param>
		public void Unsubscribe(ActivityExecutionContext parentContext,
		                        IActivityEventListener<QueueEventArgs> parentEventHandler)
		{
			WorkflowQueuingService queueService =
				(WorkflowQueuingService) parentContext.GetService(typeof (WorkflowQueuingService));
			WorkflowQueue eventQueue = queueService.GetWorkflowQueue(QueueName);
			eventQueue.UnregisterForQueueItemAvailable(parentEventHandler);
			queueService.DeleteWorkflowQueue(eventQueue.QueueName);
		}
	}
}
